package cn.hky.test;

import cn.hky.utils.ESClientUtil;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.sort.SortOrder;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

public class Estest {
    @Test
    public void testAdd() throws Exception{
        // 1.获取ES连接
        TransportClient client = ESClientUtil.getClient();
        for (int i = 1; i < 100; i++) {
            IndexRequestBuilder builder = client.prepareIndex("pethome", "employee", "1");
            builder.setId(i + "");
            HashMap<String, Object> map = new HashMap<String,Object>();
            map.put("id", i);
            if (i % 3==0){
                map.put("name", "我在玩耍--" + i);
            }else if(i % 3==1){
                map.put("name", "我在开心--" + i);
            }else {
                map.put("name", "我在害怕--" + i);
            }
            map.put("age", i);
            map.put("sex", i%2);
            // 4.得到添加文档结果
            IndexResponse indexResponse = builder.setSource(map).get();
            System.out.println(indexResponse);
        }
    }

    @Test
    public void testGet() throws Exception{
        // 1.获取ES连接
        TransportClient client = ESClientUtil.getClient();
        // 2.获取指定文档
        GetResponse response = client.prepareGet("pethome", "employee", "1").get();
        System.out.println(response);
        System.out.println(response.getSource());
        // 3.关闭连接
        client.close();
    }

    @Test
    public void testUpt() throws Exception{
        // 1.获取ES连接
        TransportClient client = ESClientUtil.getClient();
        // 2.准备要修改的数据
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("name", "我在休息!");
        // 3.使用链式编程，将要修改的数据存入到ES中
        UpdateResponse response = client.prepareUpdate("pethome", "employee", "1").setDoc(map).get();
        // 4.得到修改结果
        System.out.println(response);
        // 5.关闭连接
        client.close();
    }

    @Test
    public void testDel() throws Exception{
        // 1.获取ES连接
        TransportClient client = ESClientUtil.getClient();
        // 3.使用链式编程，删除指定文档
        DeleteResponse response = client.prepareDelete("pethome", "employee", "1").get();
        // 4.得到删除结果
        System.out.println(response);
        // 5.关闭连接
        client.close();
    }

    @Test
    public void testComplex() throws Exception {
        // 1.获取客户端连接
        TransportClient client = ESClientUtil.getClient();
        // 2.指定要操作的索引库，类型
        SearchRequestBuilder pethome = client.prepareSearch("pethome").setTypes("employee");
        // 3.设置搜索条件  当一个方法需要传递的对象是一个接口的时候，那么我们可以如何传递：

        BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();

        boolQuery.must(QueryBuilders.matchQuery("name", "我在休息"))
                .filter(QueryBuilders.termQuery("sex", 1))
                .filter(QueryBuilders.rangeQuery("age").gte(1).lte(12));

        pethome.setQuery(boolQuery);

        pethome.setSize(10);
        pethome.setFrom(0);
        pethome.addSort("age", SortOrder.DESC);

        SearchResponse searchResponse = pethome.get();

        SearchHit[] hits = searchResponse.getHits().getHits();
        for (SearchHit hit : hits) {
            System.out.println(hit.getSourceAsMap());
        }
    }
}
